package com.ruoyi.mall.controller;

import java.util.List;

import com.ruoyi.mall.entity.MallCoupons;
import com.ruoyi.mall.entity.MallPackages;
import com.ruoyi.mall.service.IMallPackagesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.mall.service.IMallCouponsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 优惠劵Controller
 *
 * @author ruoyi
 * @date 2020-05-25
 */
@Controller
@RequestMapping("/mall/coupons")
public class MallCouponsController extends BaseController {
    private String prefix = "mall/coupons";

    @Autowired
    private IMallCouponsService mallCouponsService;
    @Autowired
    private IMallPackagesService mallPackagesService;

    @RequiresPermissions("mall:coupons:view")
    @GetMapping()
    public String coupons(Long packageId, ModelMap map) {
        map.addAttribute("packageId", packageId);
        map.addAttribute("packages", getPackages());
        return prefix + "/coupons";
    }

    public List<MallPackages> getPackages() {
        return mallPackagesService.selectMallPackagesList(null);
    }

    /**
     * 查询优惠劵列表
     */
    @RequiresPermissions("mall:coupons:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MallCoupons mallCoupons,ModelMap mmap) {
        startPage();
        mmap.addAttribute("packages", getPackages());
        mmap.addAttribute("packageId", mallCoupons.getPackageId());
        List<MallCoupons> list = mallCouponsService.selectMallCouponsList(mallCoupons);
        return getDataTable(list);
    }

    /**
     * 导出优惠劵列表
     */
    @RequiresPermissions("mall:coupons:export")
    @Log(title = "优惠劵", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MallCoupons mallCoupons) {
        List<MallCoupons> list = mallCouponsService.selectMallCouponsList(mallCoupons);
        ExcelUtil<MallCoupons> util = new ExcelUtil<MallCoupons>(MallCoupons.class);
        return util.exportExcel(list, "coupons");
    }

    /**
     * 新增优惠劵
     */
    @GetMapping("/add")
    public String add(ModelMap map) {
        map.addAttribute("packages", getPackages());
        return prefix + "/add";
    }

    /**
     * 新增保存优惠劵
     */
    @RequiresPermissions("mall:coupons:add")
    @Log(title = "优惠劵", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MallCoupons mallCoupons) {
        return toAjax(mallCouponsService.insertMallCoupons(mallCoupons));
    }

    /**
     * 修改优惠劵
     */
    @GetMapping("/edit/{couponsId}")
    public String edit(@PathVariable("couponsId") Long couponsId, ModelMap mmap) {
        MallCoupons mallCoupons = mallCouponsService.selectMallCouponsById(couponsId);
        mmap.put("mallCoupons", mallCoupons);
        mmap.addAttribute("packages", getPackages());
        return prefix + "/edit";
    }

    /**
     * 修改保存优惠劵
     */
    @RequiresPermissions("mall:coupons:edit")
    @Log(title = "优惠劵", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MallCoupons mallCoupons) {
        return toAjax(mallCouponsService.updateMallCoupons(mallCoupons));
    }

    /**
     * 删除优惠劵
     */
    @RequiresPermissions("mall:coupons:remove")
    @Log(title = "优惠劵", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(mallCouponsService.deleteMallCouponsByIds(ids));
    }
}
